package com.styytech.dadabao.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by User on 2015/7/20.
 */
public class SpUser {

    /**
     * 获取存储系统设置相关的 SharedPreferences
     *
     * @param context
     * @return
     */
    private static SharedPreferences getShare(Context context) {
        SharedPreferences share = null;
        share = context
                .getSharedPreferences("USERDATA", Context.MODE_PRIVATE);
        return share;
    }

    /**
     * 退出登录，清空SharedPreferences
     *
     * @param context
     */
    public static void clearShare(Context context) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.clear();
        editor.commit();
    }

    /**
     * 存储用户名
     *
     * @param context
     * @param userName 用户名
     */
    @SuppressLint("CommitPrefEdits")
    public static void setUserName(Context context, String userName) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString("USERNAME", userName);
        editor.commit();
    }

    /**
     * 获取用户名
     *
     * @param context
     */
    public static String getUserName(Context context) {
        String userName = null;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return userName;
        }
        userName = share.getString("USERNAME", null);
        return userName;
    }

    /**
     * 存储用户Token
     *
     * @param context
     * @param userToken 登陆成功的token
     */
    @SuppressLint("CommitPrefEdits")
    public static void setUserToken(Context context, String userToken) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString("TOKEN", userToken);
        editor.commit();
    }

    /**
     * 获取用户TOKEN
     *
     * @param context
     */
    public static String getUserToken(Context context) {
        String userToken = null;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return userToken;
        }
        userToken = share.getString("TOKEN", null);
        return userToken;
    }

    //消息推送开关
    public static void setSwitchOpen(Context context, Boolean isOpen) {
        SharedPreferences share = getShare(context);
        if (share == null || isOpen == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putBoolean("OPEN", isOpen);
        editor.commit();
    }

    /**
     * 获取消息推送开关状态
     *
     * @param context
     */
    public static Boolean getSwitchOpen(Context context) {
        Boolean isOpen = false;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return isOpen;
        }
        isOpen = share.getBoolean("OPEN", false);
        return isOpen;
    }

    /**
     * 存储用户手势密码
     *
     * @param context
     * @param userGesturePassword 手势密码
     */
    @SuppressLint("CommitPrefEdits")
    public static void setUserGesturePassword(Context context, String userGesturePassword) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString("GESTUREPASSWORD", userGesturePassword);
        editor.commit();
    }

    /**
     * 获取用户手势密码
     *
     * @param context
     */
    public static String getUserGesturePassword(Context context) {
        String userGesturePassword = null;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return userGesturePassword;
        }
        userGesturePassword = share.getString("GESTUREPASSWORD", null);
        return userGesturePassword;
    }

    /**
     * 存储用户手势密码开关
     *
     * @param context
     * @param gestureSwitch 手势密码
     */
    @SuppressLint("CommitPrefEdits")
    public static void setGestureSwitch(Context context, boolean gestureSwitch) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putBoolean("GESTURESWITCH", gestureSwitch);
        editor.commit();
    }

    /**
     * 获取用户手势密码开关
     * 默认是开的
     *
     * @param context
     */
    public static boolean getGestureSwitch(Context context) {
        boolean gestureSwitch = true;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return gestureSwitch;
        }
        gestureSwitch = share.getBoolean("GESTURESWITCH", true);
        return gestureSwitch;
    }

    //设置实名认证状态
    public static void setIdCardVStatus(Context context, int isOpen) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putInt("IdCardVStatus", isOpen);
        editor.commit();
    }

    /**
     * 获取实名认证状态
     *
     * @param context
     */
    public static int getIdCardVStatus(Context context) {
        int isOpen = 0;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return isOpen;
        }
        isOpen = share.getInt("IdCardVStatus", 0);
        return isOpen;
    }

    //设置绑定银行卡状态
    public static void setBankCardVStatus(Context context, int isOpen) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }

        SharedPreferences.Editor editor = share.edit();
        editor.putInt("BankCardVStatus", isOpen);
        editor.commit();
    }

    /**
     * 获取绑定银行卡状态
     *
     * @param context
     */
    public static int getBankCardVStatus(Context context) {
        int isOpen = 0;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return isOpen;
        }
        isOpen = share.getInt("BankCardVStatus", 0);
        return isOpen;
    }

    //设置支付密码状态
    public static void setBuyPasswordStatus(Context context, int isOpen) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putInt("BuyPasswordStatus", isOpen);
        editor.commit();
    }

    /**
     * 获取支付密码状态
     *
     * @param context
     */
    public static int getBuyPasswordStatus(Context context) {
        int isOpen = 0;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return isOpen;
        }
        isOpen = share.getInt("BuyPasswordStatus", 0);
        return isOpen;
    }


    /**
     * 存储用户信息
     *
     * @param context
     * @param key     SharedPreferences key
     * @param value   数值
     * @deprecated
     */
    public static void setUserInfo(Context context, String key, String value) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString(key, value);
        editor.commit();
    }


    /**
     * 获取用户信息
     *
     * @param context
     * @param key     SharedPreferences key
     * @return
     */
    public static String getUserInfo(Context context, String key) {
        String value = null;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return value;
        }
        value = share.getString(key, null);
        return value;
    }

    //设置手机绑定状态
    public static void setMobileVStatus(Context context, int isOpen) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putInt("mobileVStatus", isOpen);
        editor.commit();
    }

    /**
     * 获取手机绑定状态
     *
     * @param context
     */
    public static int getMobileVStatus(Context context) {
        int isOpen = 0;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return isOpen;
        }
        isOpen = share.getInt("mobileVStatus", 0);
        return isOpen;
    }

    //设置绑定手机
    public static void setMobileNumer(Context context, String phone) {
        SharedPreferences share = getShare(context);
        if (share == null && phone == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString("mobile", phone);
        editor.commit();
    }

    /**
     * 获取绑定手机
     *
     * @param context
     */
    public static String getMobileNumer(Context context) {
        String phone = "";
        SharedPreferences share = getShare(context);
        if (share == null) {
            return phone;
        }
        phone = share.getString("mobile", "");
        return phone;
    }
    //存储工号
    public static void setUserJobNum(Context context, String jobNum) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString("JOBNUM", jobNum);
        editor.commit();
    }

    /**
     * 获取工号
     *
     * @param context
     */
    public static String getUserJobNum(Context context) {
        String userToken = null;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return userToken;
        }
        userToken = share.getString("JOBNUM", null);
        return userToken;
    }
    //存储用户身份
    public static void setUserType(Context context, int jobNum) {
        SharedPreferences share = getShare(context);
        if (share == null) {
            return;
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putInt("userType", jobNum);
        editor.commit();
    }

    /**
     * 获取用户身份
     *
     * @param context
     */
    public static int getUserType(Context context) {
        int userToken = 1;
        SharedPreferences share = getShare(context);
        if (share == null) {
            return userToken;
        }
        userToken = share.getInt("userType", 1);
        return userToken;
    }
}
